package com.example.administrator.printoutindemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dothantech.lpapi.IAtBitmap;
import com.dothantech.printer.IDzPrinter;
import com.example.administrator.printoutindemo.base.BaseActivity;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.example.administrator.printoutindemo.zxing.activity.CaptureActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ToastUtil;
import com.xiaoxing.zhinenghuacangku.mvp.ui.utils.PrinterUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

/**
 * Created by Administrator on 2019/3/9.
 */

public class FengqianActivity extends BaseActivity {
    private PrinterUtil mPrinterUtil;
    private ImageView iv_qysaomiao;
    private TextView tv_quyangdizhi, tv_wuliaoname, tv_quyangcount, tv_danweiguige, tv_check, tv_dayinerweima;
    private String scanResult = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fengqian);
        mPrinterUtil = new PrinterUtil(this);
        IDzPrinter.Factory.getInstance().init(this, mPrinterUtil.mCallback);

        iv_qysaomiao = (ImageView) findViewById(R.id.iv_qysaomiao);
        tv_quyangdizhi = (TextView) findViewById(R.id.tv_quyangdizhi);
        tv_wuliaoname = (TextView) findViewById(R.id.tv_wuliaoname);
        tv_quyangcount = (TextView) findViewById(R.id.tv_quyangcount);
        tv_danweiguige = (TextView) findViewById(R.id.tv_danweiguige);
        tv_check = (TextView) findViewById(R.id.tv_check);
        tv_dayinerweima = (TextView) findViewById(R.id.tv_dayinerweima);
        tv_dayinerweima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createMaskCode();
            }
        });
        tv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simpleMaskSuccess();
            }
        });
        iv_qysaomiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent openCameraIntent = new Intent(FengqianActivity.this,
                        CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
            }
        });

    }

    private void createMaskCode() {


        String methodName = "CreateMaskCode";

        SoapParams params = new SoapParams();
        params.put("simpleCode", scanResult);


        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    printQRcode(result);
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());
    }

    private void simpleMaskSuccess() {


        String methodName = "SimpleMaskSuccess";

        SoapParams params = new SoapParams();
        params.put("simpleCode", scanResult);


        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    SPUtil.putString(FengqianActivity.this, "fengqianbianma", result);
                    ToastUtil.showMessage(FengqianActivity.this, "受理成功！");
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());
    }

    private void printQRcode(String content) {
        if (mPrinterUtil.selectPrinterCheck()) {

            // 得到 IDzPrinter 对象
            IDzPrinter printer = IDzPrinter.Factory.getInstance();
            // 连接配对的第一个打印机对象
            printer.connect(IDzPrinter.Factory.getFirstPrinter());
            // 创建 IAtBitmap 绘图对象
            IAtBitmap api = IAtBitmap.Factory.createInstance();
            // 开始绘图任务，传入参数(页面宽度, 页面高度)
            api.startJob(48 * 100, 50 * 100);//48, 50
            // 开始一个页面的绘制，绘制二维码
            api.draw2DQRCode(content, 10 * 100, 5 * 100, 25 * 100);//  9, 10, 20,30

            // 结束绘图任务
            api.endJob();
            // 打印
            printer.print(api, null);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            scanResult = bundle.getString("result");
            if (scanResult != null | TextUtils.isEmpty(scanResult)) {
                simpleMaskerGetInfo();

            }
        }
    }

    private void simpleMaskerGetInfo() {


        String methodName = "SimpleMaskerGetInfo";

        SoapParams params = new SoapParams();
        params.put("simpleCode", scanResult);


        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    JSONObject jsonObject = JSON.parseObject(result);
                    String qydd = jsonObject.getString("simpleGetAddress");
                    String qysl = jsonObject.getString("spotnum");
                    String wlmc = jsonObject.getString("material");
                    String dwgg = jsonObject.getString("unit");
                    tv_danweiguige.setText(dwgg);
                    tv_quyangcount.setText(qysl);
                    tv_quyangdizhi.setText(qydd);
                    tv_wuliaoname.setText(wlmc);
                    SPUtil.putString(FengqianActivity.this, "quyangerweima", jsonObject.getString("mid"));
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());
    }
}
